﻿package com.PlantVsZombies.pass;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.PlantVsZombies.constant.Constant;
import com.PlantVsZombies.util.JsonUtil;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
/**
 * 关卡功具
 * @author wangdongdong
 *
 */
public class PassBuilder {
	/**
	 * 数组转为关卡
	 * 
	 * @return
	 */
	public static List<PassMeta> arrayToPass(int[][][] pass) {
		List<PassMeta> metas = new ArrayList<>();

		for (int i = 0; i < pass.length; i++) {
			PassMeta meta = new PassMeta();
			List<PassGroup> groups = new ArrayList<>();
			for (int j = 0; j < pass[i].length; j++) {
				PassGroup group = new PassGroup();
				group.setZombies(pass[i][j]);
				groups.add(group);
			}
			meta.setGroups(groups);
			metas.add(meta);
		}
  
		return metas;
	}
	
	/**
	 * 关卡转数组
	 * @param metas
	 * @return
	 */
	public static int [][][] passToArray(List<PassMeta> metas) {
		int totalPass = metas.size();
		int [][][] pass = new int[totalPass][][];
		for (int i=0; i<totalPass; i++) {
			List<PassGroup> groups = metas.get(i).getGroups();
			pass [i] = new int[groups.size()][];
			for (int j=0; j< groups.size(); j++) {
				int[] zombies = groups.get(j).getZombies();
				pass [i][j] = new int[zombies.length];
				pass [i][j] = zombies;
			}
		}
		return pass;
	}
	
	/**
	 * 关卡转为json字符串
	 * @param metas
	 * @return
	 */
	public static String passToJsonString(List<PassMeta> metas){
		return JsonUtil.toJSONString(metas);
	}
	
	/**
	 * json字符串转为关卡
	 * @param jsonString
	 * @return
	 */
	public static List<PassMeta> jsonToPass(String jsonString) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setRootClass(PassMeta.class);
		Map<String, Class> classMap = new HashMap<String, Class>();
		classMap.put("groups", PassGroup.class);
		jsonConfig.setClassMap(classMap);
		JSONArray jsonArray = JsonUtil.toJSONArray(jsonString);
		return JsonUtil.toList(jsonArray, jsonConfig);
	}
	
	/**
	 * 获取关卡总僵尸数量
	 * @param passMeta
	 * @return
	 */
	public static int getZombieNumber(PassMeta passMeta) {
		int total = 0;
		for (PassGroup group : passMeta.getGroups()) {
			total += group.getCount();
		}
		return total;
	}

	/**
	 * 判断 当前是不是关卡最后一波
	 * @param group
	 * @return
	 */
	public static boolean isSecondLast(PassGroup group, PassMeta passMeta) {
		List<PassGroup> groups = passMeta.getGroups();
		return groups.get(groups.size()-1) == group ? true : false;
	}
	
	/**
	 * 数组中是否包含数值
	 * @param array
	 * @param value
	 * @return
	 */
	public static boolean isContain(int [] array, int value){
		for (int i : array) {
			if (i == value) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 验证关卡信息是否正确
	 * @param metas
	 * @return
	 */
	public static boolean validatePass (List<PassMeta> metas) {
		if (metas == null) {
			return false;
		}
		for (PassMeta meta : metas) {
			if (meta == null) {
				return false;
			}
			if (meta.getGroupCount() != meta.getGroups().size()) {
				return false;
			}
			for (PassGroup group : meta.getGroups()){
				if (group == null && group.getZombies().length < 1){
					return false;
				}
			}
		}
		return true;
	}
	
	public static void main(String[] args) {
		List<PassMeta> metas = arrayToPass(Constant.PASS);
		System.out.println(validatePass(metas));
		System.out.println(Arrays.toString(passToArray(metas)));
		/*String jsonString = JsonUtil.toJSONString(metas);
		System.out.println(jsonString);
		JSONArray jsonArray = JsonUtil.toJSONArray(jsonString);
		List<PassMeta> list = JsonUtil.toList(jsonArray, PassMeta.class);
		System.out.println(JsonUtil.toJSONString(list));*/
	}
}
